'use strict';

var	async = require('async'),
	db = require('../database'),
	plugins = require('../plugins');

module.exports = function(Groups) {

	Groups.ownership = {};

	Groups.ownership.isOwner = function(uid, groupName, callback) {
		if (!uid) {
			return callback(null, false);
		}
		db.isSetMember('group:' + groupName + ':owners', uid, callback);
	};

	Groups.ownership.isOwners = function(uids, groupName, callback) {
		if (!Array.isArray(uids)) {
			return callback(null, []);
		}

		db.isSetMembers('group:' + groupName + ':owners', uids, callback);
	};

	Groups.ownership.grant = function(toUid, groupName, callback) {
		// Note: No ownership checking is done here on purpose!
		async.waterfall([
			function(next) {
				db.setAdd('group:' + groupName + ':owners', toUid, next);
			},
			function(next) {
				plugins.fireHook('action:group.grantOwnership', {uid: toUid, groupName: groupName});
				next();
			}
		], callback);
	};

	Groups.ownership.rescind = function(toUid, groupName, callback) {
		// Note: No ownership checking is done here on purpose!

		// If the owners set only contains one member, error out!
		async.waterfall([
			function (next) {
				db.setCount('group:' + groupName + ':owners', next);
			},
			function (numOwners, next) {
				if (numOwners <= 1) {
					return next(new Error('[[error:group-needs-owner]]'));
				}
				db.setRemove('group:' + groupName + ':owners', toUid, next);
			},
			function (next) {
				plugins.fireHook('action:group.rescindOwnership', {uid: toUid, groupName: groupName});
				next();
			}
		], callback);
	};
};
